<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class GoodsSpecItem extends Model
{
    protected $table = 'ims_yz_goods_spec_item';
    
    protected $fillable = [
        'uniacid',
        'specid',
        'title',
        'thumb',
        'show',
        'display_order',
        'valueId',
        'virtual',
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    protected $casts = [
        'uniacid' => 'integer',
        'specid' => 'integer',
        'show' => 'integer',
        'display_order' => 'integer',
        'virtual' => 'integer',
        'created_at' => 'integer',
        'updated_at' => 'integer',
        'deleted_at' => 'integer'
    ];

    /**
     * 获取商品规格
     */
    public function spec(): BelongsTo
    {
        return $this->belongsTo(GoodsSpec::class, 'specid', 'id');
    }

    /**
     * 查询显示的规格项
     */
    public function scopeVisible($query)
    {
        return $query->where('show', 1);
    }

    /**
     * 按排序查询
     */
    public function scopeOrdered($query)
    {
        return $query->orderBy('display_order', 'asc')->orderBy('id', 'desc');
    }

    /**
     * 获取显示状态文本
     */
    public function getShowStatusTextAttribute(): string
    {
        return $this->show ? '显示' : '隐藏';
    }

    /**
     * 获取虚拟状态文本
     */
    public function getVirtualStatusTextAttribute(): string
    {
        return $this->virtual ? '虚拟' : '实体';
    }
}
